package uk.co.cocking.getinline2.pipeline.transformers;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import uk.co.cocking.getinline2.record.Record;

public class CsvFormatter extends AbstractTransformer<Record, String> {

	public List<String> transform(Record record) {
		Object[] values = new Object[record.fieldNames().size()];
		int index = 0;
		for (String fieldName : record.fieldNames()) {
			values[index++] = enquote(record.getString(fieldName));
		}
		return enlist(StringUtils.join(values, ", "));
	}

	private String enquote(String string) {
		return "\"" + string.replace("\"", "\"\"") + "\"";
	}

}
